package com.woniuxy.service.impl;

import com.woniuxy.entity.Menu;
import com.woniuxy.mapper.MenuMapper;
import com.woniuxy.service.MenuService;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dingpf
 * @version 1.0
 * @description
 * @create 2024/8/23 17:20
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询用户的菜单信息
     * 1: 找出所有父菜单
     * 找出parentId = -1
     * 2：让每个父菜单找出自己子菜单
     * 循环父菜单去 所有菜单的集合找（父菜单的id == 菜单的parentId）
     *
     * @return
     */
    @Override
    public ResponseData<?> queryPowerUserMenu() {
        Long userId = UserContext.getUserId();
        List<Menu> menus = menuMapper.queryPowerUserMenu(userId.intValue());

        List<Menu> allMenus = menuMapper.queryAllMenu();


        for (Menu menu : menus) {
            ArrayList<Menu> children = new ArrayList<>();
            for (Menu allMenu : allMenus) {
                if (menu.getId().equals(allMenu.getParentId())) {
                    children.add(allMenu);
                }
                menu.setChildrenMenu(children);
            }
        }


        return new ResponseData<>().ok(menus);
    }

    @Override
    public ResponseData<?> queryPowerUserAuthByUserId(Integer id) {
        return new ResponseData<>().ok(menuMapper.queryPowerUserAuthByUserId(id));
    }
}
